<?php
namespace App\Http\ViewComposers;
//use Illuminate\Contracts\View\View;
use App\Models\Admin\AdminUserRole;
use Illuminate\View\View;

class IndexComposer
{

    public function compose(View $view)
    {
        //获取当前登录用户的权限，根据session取出用户id值
//        $id = session('admin')->id;
        $admin_user = session('admin_user');
        $id = $admin_user['id'];
        $permissions = AdminUserRole::select('admin_permissions.*')
            ->LeftJoin('admin_role_permission', 'admin_role_permission.role_id', 'admin_user_role.role_id')
            ->LeftJoin('admin_permissions', 'admin_permissions.id', 'admin_role_permission.permission_id')
            ->where('admin_user_role.user_id', $id)
            ->where('admin_permissions.is_menu', 1)
            ->groupBy('admin_permissions.id')
            ->get();
//        dd($permissions);

        //取出权限名称，并以树状图存入数组
        $tree = array();
//        dd($tree);
        foreach ($permissions as $permission) {
//            dd($permissions);
            if ($permission->parent_id == 0) {
                foreach ($permissions as $v)
//                    dd($permissions);
//                    dd($v);
                    if ($permission->id == $v->parent_id) {
                        $tree[$permission->name][] = $v;
//                        dd($tree);
                    }
            }
        }
//        dd($permissions);
//        dd($tree);
        // 使用基于闭包的composers...
//        view()->composer('master', function ($view) use ($tree) {
//            $view->with('tree', $tree);
        $view->with('tree', $tree)->with('id', $id);
//        });
    }



}
